package com.parking.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.StateData;
import com.parking.order.model.OrderLogs;
import com.parking.order.dao.OrderLogsDao;
import com.parking.order.model.dto.OrderLogsDto;
import com.parking.order.model.exception.OrderException;
import com.parking.order.model.exception.OrderExceptionCode;
import com.parking.order.model.param.FindOrderLogsParam;
import com.parking.order.service.IOrderLogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.order.util.BasicInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>
 *  订单日志服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@Service
public class OrderLogsServiceImpl extends ServiceImpl<OrderLogsDao, OrderLogs> implements IOrderLogsService {
    /**
     * 订单日志数据层
     * */
    @Autowired
    private OrderLogsDao orderLogsDao;
    /**
     * 其他模块调用工具类
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;
    /**
     * 查询订单日志
     * 参数 物业Id certifiedId
     *     平台Id platformId
     *     开始时间 startTime
     *     结束时间 endTime
     *     订单编号 orderInfoNum
     *     日志人类型 logsPersonType
     *     日志人 logsUpdatePerson
     *     页数   pageNum
     *     每页大小 pageSize
     *
     * 返回 订单编号 orderInfoNum
     *      日志时间 logsUpdateTime
     *      日志人类型 logsPersonType
     *      日志人 logsUpdatePerson
     *      日志描述 orderLogsDescribe
     * */
    @Override
    public PageInfo findOrderLogs(FindOrderLogsParam param) throws Exception {
        System.out.println("=========================查询订单日志=========================");
        System.out.println("页数:"+param.getPageNum());
        System.out.println("每页大小:"+param.getPageSize());
        System.out.println("开始时间:"+param.getStartTime());
        System.out.println("结束时间:"+param.getEndTime());
        System.out.println("订单编号:"+param.getOrderInfoNum());
        System.out.println("日志人类型:"+param.getLogsPersonType()+"(0系统自动 1用户 2物业)");
        System.out.println("日志人:"+param.getLogsUpdatePerson());
        System.out.println("平台Id:"+param.getPlatformId());
        System.out.println("物业Id:"+param.getCertifiedId());


        boolean isPlatformId = param.getPlatformId()!=null;
        boolean isCertifiedId = param.getCertifiedId()!=null;
        boolean isStartTime = param.getStartTime()!=null;
        boolean isEndTime = param.getEndTime()!=null;
        boolean isOrderInfoNum = param.getOrderInfoNum()!=null;
        boolean isLogsPersonType = param.getLogsPersonType()!=null;
        boolean isLogsUpdatePerson = param.getLogsUpdatePerson()!=null&&!param.getLogsUpdatePerson().equals("");

        if(isPlatformId) basicInfoUtil.getPlatform(param.getPlatformId());
        Integer certifiedInfoId = null;
        if(isCertifiedId) {
            certifiedInfoId = basicInfoUtil.getCertifiedAccount(param.getCertifiedId()).getCertifiedInfoId();
        }
        if(isStartTime&&isEndTime&&param.getEndTime().isBefore(param.getStartTime())) {
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_START_NOT_BEFORE_END);
        }

        Page<OrderLogs> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<OrderLogs> wrapper = new QueryWrapper<>();
        if(certifiedInfoId!=null) wrapper.eq("certified_info_id",certifiedInfoId);
        if(isStartTime) wrapper.ge("logs_update_time",param.getStartTime());
        if(isEndTime) wrapper.le("logs_update_time",param.getEndTime());
        if(isOrderInfoNum) wrapper.eq("order_info_num",param.getOrderInfoNum());
        if(isLogsPersonType) wrapper.eq("logs_person_type",param.getLogsPersonType());
        if(isLogsUpdatePerson) wrapper.eq("logs_update_person",param.getLogsUpdatePerson());
        wrapper.orderByDesc("logs_update_time");
        orderLogsDao.selectPage(page,wrapper);

        ArrayList<OrderLogsDto> dtos = new ArrayList<>();
        for (OrderLogs log : page.getRecords()) {
            OrderLogsDto dto = BeanUtil.toBean(log, OrderLogsDto.class);
            String logsPersonType = null;
            if(Objects.equals(log.getLogsPersonType(), StateData.ORDER_LOG_PERSON_TYPE_SYSTEM)) logsPersonType = "系统";
            if(Objects.equals(log.getLogsPersonType(), StateData.ORDER_LOG_PERSON_TYPE_USER)) logsPersonType = "用户";
            if(Objects.equals(log.getLogsPersonType(), StateData.ORDER_LOG_PERSON_TYPE_CERTIFIED)) logsPersonType = "物业";
            dto.setLogsPersonType(logsPersonType);
            dtos.add(dto);
        }

        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRecords(dtos);
        return pageInfo;
    }
}
